/*
 * Progetto, a new java application 
 *
 * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Rattazzi Street, Fifth Floor
 * Pomezia, RM  00040  Italy
 */
package com.jfootball.business.impl;

import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jfootball.dao.StaffDao;
import com.jfootball.dao.TeamDao;
import com.jfootball.domain.Staff;
import com.jfootball.domain.Team;
import com.jfootball.service.StaffService;

/**
 * @author C_ICTDNS
 *
 */
@Transactional(readOnly = true)
@Service("staffService")
@RemoteProxy(name = "staffService")
public class StaffServiceImpl implements StaffService
{
	private StaffDao staffDAO;
	private TeamDao teamDAO;
	
	@Autowired
	public StaffServiceImpl(StaffDao staffDAO, TeamDao teamDAO) 
	{
		this.staffDAO = staffDAO;
		this.teamDAO = teamDAO;
	}		

	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void saveStaff(Staff obj)
	{
		if (obj.getImage() == null && obj.getId() != null)
		{
			Long id = obj.getId();
			Staff m = getStaffByID(id);
			byte[] image = m.getImage();
			obj.setImage(image);
		}	
		staffDAO.saveStaff(obj);
	}

	@RemoteMethod
	public List<Staff> getStaffsByLetter(String letter, String searchType)
	{
		return staffDAO.listStaffsByLetter(letter, searchType);
	}	
	

	public List<Staff> getStaffsByTeam(Long teamId)
	{

		return staffDAO.listStaffsByTeam(teamId);
	}	
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	public void deleteStaff(Long staffId)
	{
		staffDAO.deleteStaff(staffId);
	}

	@RemoteMethod
	public Staff getStaffByID(Long staffId)
	{
		Staff m = staffDAO.getStaffById(staffId);
		return m;
	}
	
	@Transactional(readOnly = false)
	public void updateStaffById(String staffId)
	{
		Staff staff = getStaffByID(new Long((String)staffId));
		
		// fine 
		staff.setRetired(false);
		staff.setUnemployed(false);		
		
		staffDAO.saveStaff(staff);
	}
	
	public void updatePlayer(String staffId, String teamId, String status)
	{
		Staff staff = getStaffByID(new Long(staffId));
		Team team = teamDAO.getTeamByID(new Long(teamId));
		
		// fine 
		staff.setRetired(false);
		staff.setUnemployed(false);
		
		staff.setTeam(team);
		staffDAO.saveStaff(staff);
	}
	
	@RemoteMethod
	public boolean findStaffExists(String firstName, String lastName, String dateOfBirth)
	{
		return staffDAO.findStaffExists(firstName, lastName, dateOfBirth);
	}
	

	public Staff getStaffByTeam(Long teamId) {
		return staffDAO.getStaffBySecondId(teamId);
	}	
	


}
